package com.cxd.web.frontend;

import com.cxd.entity.EbComment;
import com.cxd.service.IEbCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/f-comment")
public class EbCommentController {
    @Autowired
    private IEbCommentService iEbCommentService;

    @RequestMapping("/index")
    public String list(HttpSession session, Model model, @RequestParam(value="page",defaultValue = "1")int page, @RequestParam(value="pageSize",defaultValue = "10")int pageSize){
        Object userNow = session.getAttribute("lgnUser");
        if (userNow == null){
            return "redirect:/f-login/to_login";
        }
        model.addAttribute("userNow",userNow);
        List<EbComment> ebComments=iEbCommentService.listByPage(page,pageSize);
        final Integer totalPage=iEbCommentService.findTotalPage(pageSize);
        model.addAttribute("ebcomments",ebComments);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currPage",page);
//        System.out.println("EbCommentController==分页数据wei："+ebComments);
//        System.out.println(model);
        return "guestbook";
    }

    @RequestMapping("/to-save")
    public String toSave(HttpSession session,Model model){
        Object userNow = session.getAttribute("lgnUser");
        if (userNow == null){
            return "redirect:/f-login/to_login";
        }
        model.addAttribute("userNow",userNow);
        return "/common/comment-save";
    }

    @RequestMapping("/save")
    public String save(HttpSession session,EbComment ebComment,Model model){
//        System.out.println("得到"+ebComment);
        Object userNow = session.getAttribute("lgnUser");
        if (userNow == null){
            return "redirect:/f-login/to_login";
        }
        model.addAttribute("userNow",userNow);
        Integer result = iEbCommentService.save(ebComment);
        return  "/common/comment-save";
    }
}
